package com.sunsys.hdfs.compression.gzip;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.io.compress.CompressionCodec;
import org.apache.hadoop.io.compress.CompressionOutputStream;
import org.apache.hadoop.util.ReflectionUtils;

public class Compress {

	/**
	 *	This program reads the text from the Keyboard and passes to the CompressionOutputStream, which compress and prints on the Consolel 
	 * 
	 */
	
	
	public static void main(String []args) {
	
		Configuration configuration = null;
		CompressionCodec compressionCodec = null;
		String codeClassName = "org.apache.hadoop.io.compress.GzipCodec";
		Class<?> codecClass = null;
		
		try {
			/* create Configuration instance */
			configuration = new Configuration();
			/* load the Codec Class */
			codecClass = Class.forName(codeClassName);
			/* Get an instance of Codec class */
			compressionCodec = (CompressionCodec) ReflectionUtils.newInstance(codecClass, configuration);
			
			CompressionOutputStream compressionOutputStream = compressionCodec.createOutputStream(System.out);
			
			IOUtils.copyBytes(System.in, compressionOutputStream, configuration,false);
			/* call finish() method, so that compressioOutputStream writes the output - but stream won't be closed*/
			compressionOutputStream.finish();
			
		}catch(ClassNotFoundException e1) {
			e1.printStackTrace();
		}catch(IOException e2) {
			e2.printStackTrace();
		}
	}	
}
